import { reportForms } from "@/api/index";
import { table } from "@/api";
import { mapState, mapActions } from "vuex";
import { columnsScreen, derives } from "./columns";
import RmsDetail from "@components/table/dialog/RmsDetail"
import AreaSelect from "@basic/area/AreaCodeSelect.vue";
export default {
  name: "rmsDetails",
  data () {
    return {
      total: 0,
      pageSizeOptions: [10, 20, 50, 100, 500],
      pageNo: 1,
      pageSize: 10,
      combineTr: 7,
      dialogVisible: false,
      loading: false,
      particularYear: '',
      time: '',
      pickDate: '',
      currentYear: '',
      currentMonth: '',
      currentTime: '',
      fromProvinceCode: '',
      fromCityCode: '',
      fromDistrictCode: '',
      fromTime: [],
      params: {
        fromAddress: '',
        oemGysCode: '',
        page: 1,
        size: 10,
      },
      pickerOptions: {
        onPick: this.getPickDate,
        disabledDate: this.disabledDate
      },
      derive: {},
      shipData: {},
      tableData: [],
      columns: [],
      carrierOptions: [],
      pickShortOptions: [],
      ModeOptions: [{
        label: '全部',
        value: ''
      }, {
        label: '零担',
        value: 1
      }, {
        label: '整车',
        value: 2
      }]
    };
  },
  computed: {
    ...mapState({
      menuId: (state) => state.menu.menu.menuId
    })
  },
  mounted () {
    this.columns = columnsScreen(this.menuId);
    this.derive = derives(this.menuId);
    setTimeout(() => {
      this.locationInHone();
    }, 1500);
  },
  methods: {
    // 是否带参
    locationInHone () {
      let now = new Date();
      this.currentYear = now.getFullYear()
      this.currentMonth = now.getMonth() + 1;
      this.currentTime = String(now.getFullYear()) + (this.currentMonth < 10 ? '0' + String(this.currentMonth) : String(this.currentMonth));
      this.carrierSearch();
    },
    // 切换页数大小
    handleSizeChange () {
      this.search(2);
    },
    // 分页
    handleCurrentChange () {
      this.search(2);
    },
    async carrierSearch () {
      let { data } = await reportForms.carrierSearchs({}, '1')
      let datas = data.data
      let code = data.code
      if (code === 0) {
        this.carrierOptions = datas;
      } else {
        this.carrierOptions = []
      }
    },
    async carrierChange (e) {
      this.pickShortOptions = []
      this.params.oemGysCode = ''
      let { data } = await reportForms.carrierSearchs({ supplierCompanyId: e }, '2')
      let datas = data.data
      let code = data.code
      if (code === 0) {
        this.pickShortOptions = datas;
      } else {
        this.pickShortOptions = []
      }
    },
    // 查询
    async search (type) {
      if (this.fromTime.length == 0) {
        this.$message.warning("请选择发货时间");
        return
      }
      this.params.page = type == 1 ? 1 : this.pageNo,
        this.pageNo = type == 1 ? 1 : this.pageNo,
        this.params.size = this.pageSize
      this.tableData = []
      this.loading = true
      let { data } = await reportForms.rmsList(this.params, this.menuId)
      let datas = data.data
      let code = data.code
      if (code === 0) {
        this.loading = false
        this.tableData = datas.records;
      } else {
        this.loading = false
        this.tableData = []
      }
      this.total = datas.total;
    },
    clearYear (e) {
      this.params.year = e;
    },
    // 导出
    async handleExport () {
      if (!this.tableData.length) {
        this.$message.warning("没有可要导出的数据！");
        return false;
      }
      let args = JSON.parse(JSON.stringify(this.params))
      args.page = 1
      args.size = this.total
      let params = {
        args: { ...args },
        name: this.derive.name,
        type: this.derive.type
      };
      await table.submitFormContainer(`/export/task`, params);
      this.searchCount();
      this.$nextTick(() => {
        this.$message.success("导出功能正在后台执行中，请稍后去“导出文件”下载中查看！");
      });
    },
    // 查询导出未读数
    ...mapActions({
      searchCount: "login/exportCount"
    }),
    // 清除
    resetForm () {
      this.time = []
      this.params = {
        fromAddress: '',
        page: 1,
        size: 10,
      }
      this.fromTime = '';
      this.fromProvinceCode = '';
      this.fromCityCode = '';
      this.fromDistrictCode = '';
      this.particularYear = '';
      this.locationInHone();
      this.search(2);
    },
    dialog (data) {
      this.dialogVisible = true;
      this.shipData = data;
    },
    handleClose () {
      this.dialogVisible = false;
    },
    //限制筛选时效一年
    getPickDate (pick) {
      this.pickDate = pick;
    },
    // 地区
    selectFromArea (val) {
      if (val) {
        this.params.fromProvinceCode = val.provinceCode;
        this.params.fromCityCode = val.cityCode;
        this.params.fromDistrictCode = val.districtCode;
      } else {
        delete this.params.fromProvinceCode
        delete this.params.fromCityCode
        delete this.params.fromDistrictCode
      }
    },
    //时间
    fromTimeGive (val) {
      console.log(val)
      this.params.queryStartTime = val ? val[0] : ''
      this.params.queryEndTime = val ? val[1] : ''
    },
    disabledDate (date) {
      const { minDate, maxDate } = this.pickDate;
      if (minDate && !maxDate) {
        const diff = Math.abs(minDate.valueOf() - date.valueOf());
        if (diff > 1000 * 3600 * 24 * 365) {
          return true;
        }
      }
    },
    toFixeTwoPercent (num) {
      // 小数转百分比，保留两位小数
      num = typeof num === 'undefined' ? 0 : num
      num = num * 100 + ''
      if (num.indexOf('.') === -1) {
        num = num + '.00%'
      } else if (num.split('.')[1].length === 1) {
        num = num + '0%'
      } else {
        num = (num * 1).toFixed(2) + '%'
      }
      return num
    },
    digitalConversion (data) {
      let value
      if (data == 100) {
        value = data + '%'
      } else if (data == 0 || !data) {
        value = '0%'
      } else {
        value = data.toFixed(2) + '%'
      }
      return value
    },
    sortChange (val) {
      let name = val.prop
      this.params.sort = []
      this.params.sort = [{
        asc: false,
        name: ''
      }]
      if (val.order == 'descending') {
        this.params.sort[0].asc = false
        this.params.sort[0].name = val.prop
      } else if (val.order == 'ascending') {
        this.params.sort[0].asc = true
        this.params.sort[0].name = val.prop
      } else {
        delete this.params.sort
      }
      this.search(1);
    },
    fixedProp (name) {
      let sortable
      if (name == '异常单号') {
        sortable = 'right'
      } else if (name == '体积（方）') {
        sortable = 'right'
      } else if (name == '重量（吨）') {
        sortable = 'right'
      } else if (name == '数量') {
        sortable = 'right'
      } else if (name == '异常数') {
        sortable = 'right'
      } else {
        sortable = false
      }
      return sortable
    },
    transition (val, text) {
      let value
      let values
      if (text == 'number') {
        value = val ? val : '0'
      } else if (text == 'string') {
        value = val ? val : '--'
      } else if (text == 'unit') {
        if (val) {
          value = val
        } else {
          value = '0.00'
        }
      } else if (text == 'volume') {
        if (val) {
          value = Number(val).toFixed(2)
        } else {
          value = '0.00'
        }
      } else if (text == 'weight') {
        if (val) {
          value = Number(val).toFixed(3)
          // value = values.substring(0,values.length-2)
        } else {
          value = '0.000'
        }
      } else if (text == 'fee') {
        value = val ? val : '0.00'
      } else if (text == 'time') {
        value = val ? val.substr(0, 10) : '--'
      } else {
        value = val
      }
      return value
    }
  },
  components: {
    RmsDetail,
    AreaSelect
  }
};